package cn.ibizlab.assetmanagement.assetapi.rest;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.math.BigInteger;
import java.util.HashMap;
import lombok.extern.slf4j.Slf4j;
import com.alibaba.fastjson.JSONObject;
import javax.servlet.ServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.http.HttpStatus;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.util.StringUtils;
import org.springframework.context.annotation.Lazy;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.access.prepost.PostAuthorize;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import cn.ibizlab.assetmanagement.assetapi.dto.*;
import cn.ibizlab.assetmanagement.assetapi.mapping.*;
import cn.ibizlab.assetmanagement.core.plan.domain.EAMPlanCDT;
import cn.ibizlab.assetmanagement.core.plan.service.IEAMPlanCDTService;
import cn.ibizlab.assetmanagement.core.plan.filter.EAMPlanCDTSearchContext;
import cn.ibizlab.assetmanagement.util.annotation.VersionCheck;

@Slf4j
@Api(tags = {"维护计划条件" })
@RestController("AssetApi-eamplancdt")
@RequestMapping("")
public class EAMPlanCDTResource {

    @Autowired
    public IEAMPlanCDTService eamplancdtService;

    @Autowired
    @Lazy
    public EAMPlanCDTMapping eamplancdtMapping;

    @PreAuthorize("hasPermission(this.eamplancdtMapping.toDomain(#eamplancdtdto),'iBizAssetManagement-EAMPlanCDT-Create')")
    @ApiOperation(value = "新建维护计划条件", tags = {"维护计划条件" },  notes = "新建维护计划条件")
	@RequestMapping(method = RequestMethod.POST, value = "/eamplancdts")
    @Transactional
    public ResponseEntity<EAMPlanCDTDTO> create(@RequestBody EAMPlanCDTDTO eamplancdtdto) {
        EAMPlanCDT domain = eamplancdtMapping.toDomain(eamplancdtdto);
		eamplancdtService.create(domain);
        EAMPlanCDTDTO dto = eamplancdtMapping.toDto(domain);
		return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(this.eamplancdtMapping.toDomain(#eamplancdtdtos),'iBizAssetManagement-EAMPlanCDT-Create')")
    @ApiOperation(value = "批量新建维护计划条件", tags = {"维护计划条件" },  notes = "批量新建维护计划条件")
	@RequestMapping(method = RequestMethod.POST, value = "/eamplancdts/batch")
    public ResponseEntity<Boolean> createBatch(@RequestBody List<EAMPlanCDTDTO> eamplancdtdtos) {
        eamplancdtService.createBatch(eamplancdtMapping.toDomain(eamplancdtdtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @VersionCheck(entity = "eamplancdt" , versionfield = "updatedate")
    @PreAuthorize("hasPermission(this.eamplancdtService.get(#eamplancdt_id),'iBizAssetManagement-EAMPlanCDT-Update')")
    @ApiOperation(value = "更新维护计划条件", tags = {"维护计划条件" },  notes = "更新维护计划条件")
	@RequestMapping(method = RequestMethod.PUT, value = "/eamplancdts/{eamplancdt_id}")
    @Transactional
    public ResponseEntity<EAMPlanCDTDTO> update(@PathVariable("eamplancdt_id") String eamplancdt_id, @RequestBody EAMPlanCDTDTO eamplancdtdto) {
		EAMPlanCDT domain  = eamplancdtMapping.toDomain(eamplancdtdto);
        domain .setEamplancdtid(eamplancdt_id);
		eamplancdtService.update(domain );
		EAMPlanCDTDTO dto = eamplancdtMapping.toDto(domain );
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(this.eamplancdtService.getEamplancdtByEntities(this.eamplancdtMapping.toDomain(#eamplancdtdtos)),'iBizAssetManagement-EAMPlanCDT-Update')")
    @ApiOperation(value = "批量更新维护计划条件", tags = {"维护计划条件" },  notes = "批量更新维护计划条件")
	@RequestMapping(method = RequestMethod.PUT, value = "/eamplancdts/batch")
    public ResponseEntity<Boolean> updateBatch(@RequestBody List<EAMPlanCDTDTO> eamplancdtdtos) {
        eamplancdtService.updateBatch(eamplancdtMapping.toDomain(eamplancdtdtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PreAuthorize("hasPermission(this.eamplancdtService.get(#eamplancdt_id),'iBizAssetManagement-EAMPlanCDT-Remove')")
    @ApiOperation(value = "删除维护计划条件", tags = {"维护计划条件" },  notes = "删除维护计划条件")
	@RequestMapping(method = RequestMethod.DELETE, value = "/eamplancdts/{eamplancdt_id}")
    @Transactional
    public ResponseEntity<Boolean> remove(@PathVariable("eamplancdt_id") String eamplancdt_id) {
         return ResponseEntity.status(HttpStatus.OK).body(eamplancdtService.remove(eamplancdt_id));
    }

    @PreAuthorize("hasPermission(this.eamplancdtService.getEamplancdtByIds(#ids),'iBizAssetManagement-EAMPlanCDT-Remove')")
    @ApiOperation(value = "批量删除维护计划条件", tags = {"维护计划条件" },  notes = "批量删除维护计划条件")
	@RequestMapping(method = RequestMethod.DELETE, value = "/eamplancdts/batch")
    public ResponseEntity<Boolean> removeBatch(@RequestBody List<String> ids) {
        eamplancdtService.removeBatch(ids);
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PostAuthorize("hasPermission(this.eamplancdtMapping.toDomain(returnObject.body),'iBizAssetManagement-EAMPlanCDT-Get')")
    @ApiOperation(value = "获取维护计划条件", tags = {"维护计划条件" },  notes = "获取维护计划条件")
	@RequestMapping(method = RequestMethod.GET, value = "/eamplancdts/{eamplancdt_id}")
    public ResponseEntity<EAMPlanCDTDTO> get(@PathVariable("eamplancdt_id") String eamplancdt_id) {
        EAMPlanCDT domain = eamplancdtService.get(eamplancdt_id);
        EAMPlanCDTDTO dto = eamplancdtMapping.toDto(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @ApiOperation(value = "获取维护计划条件草稿", tags = {"维护计划条件" },  notes = "获取维护计划条件草稿")
	@RequestMapping(method = RequestMethod.GET, value = "/eamplancdts/getdraft")
    public ResponseEntity<EAMPlanCDTDTO> getDraft() {
        return ResponseEntity.status(HttpStatus.OK).body(eamplancdtMapping.toDto(eamplancdtService.getDraft(new EAMPlanCDT())));
    }

    @ApiOperation(value = "检查维护计划条件", tags = {"维护计划条件" },  notes = "检查维护计划条件")
	@RequestMapping(method = RequestMethod.POST, value = "/eamplancdts/checkkey")
    public ResponseEntity<Boolean> checkKey(@RequestBody EAMPlanCDTDTO eamplancdtdto) {
        return  ResponseEntity.status(HttpStatus.OK).body(eamplancdtService.checkKey(eamplancdtMapping.toDomain(eamplancdtdto)));
    }

    @PreAuthorize("hasPermission(this.eamplancdtMapping.toDomain(#eamplancdtdto),'iBizAssetManagement-EAMPlanCDT-Save')")
    @ApiOperation(value = "保存维护计划条件", tags = {"维护计划条件" },  notes = "保存维护计划条件")
	@RequestMapping(method = RequestMethod.POST, value = "/eamplancdts/save")
    public ResponseEntity<Boolean> save(@RequestBody EAMPlanCDTDTO eamplancdtdto) {
        return ResponseEntity.status(HttpStatus.OK).body(eamplancdtService.save(eamplancdtMapping.toDomain(eamplancdtdto)));
    }

    @PreAuthorize("hasPermission(this.eamplancdtMapping.toDomain(#eamplancdtdtos),'iBizAssetManagement-EAMPlanCDT-Save')")
    @ApiOperation(value = "批量保存维护计划条件", tags = {"维护计划条件" },  notes = "批量保存维护计划条件")
	@RequestMapping(method = RequestMethod.POST, value = "/eamplancdts/savebatch")
    public ResponseEntity<Boolean> saveBatch(@RequestBody List<EAMPlanCDTDTO> eamplancdtdtos) {
        eamplancdtService.saveBatch(eamplancdtMapping.toDomain(eamplancdtdtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','iBizAssetManagement-EAMPlanCDT-searchDefault-all') and hasPermission(#context,'iBizAssetManagement-EAMPlanCDT-Get')")
	@ApiOperation(value = "获取DEFAULT", tags = {"维护计划条件" } ,notes = "获取DEFAULT")
    @RequestMapping(method= RequestMethod.GET , value="/eamplancdts/fetchdefault")
	public ResponseEntity<List<EAMPlanCDTDTO>> fetchDefault(EAMPlanCDTSearchContext context) {
        Page<EAMPlanCDT> domains = eamplancdtService.searchDefault(context) ;
        List<EAMPlanCDTDTO> list = eamplancdtMapping.toDto(domains.getContent());
        return ResponseEntity.status(HttpStatus.OK)
                .header("x-page", String.valueOf(context.getPageable().getPageNumber()))
                .header("x-per-page", String.valueOf(context.getPageable().getPageSize()))
                .header("x-total", String.valueOf(domains.getTotalElements()))
                .body(list);
	}

    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','iBizAssetManagement-EAMPlanCDT-searchDefault-all') and hasPermission(#context,'iBizAssetManagement-EAMPlanCDT-Get')")
	@ApiOperation(value = "查询DEFAULT", tags = {"维护计划条件" } ,notes = "查询DEFAULT")
    @RequestMapping(method= RequestMethod.POST , value="/eamplancdts/searchdefault")
	public ResponseEntity<Page<EAMPlanCDTDTO>> searchDefault(@RequestBody EAMPlanCDTSearchContext context) {
        Page<EAMPlanCDT> domains = eamplancdtService.searchDefault(context) ;
	    return ResponseEntity.status(HttpStatus.OK)
                .body(new PageImpl(eamplancdtMapping.toDto(domains.getContent()), context.getPageable(), domains.getTotalElements()));
	}
}

